package com.example.springbootdemo1.demos.web;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

import static sun.net.www.protocol.http.HttpURLConnection.userAgent;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/getCookie")
    public  String getCookie(HttpServletRequest request){
        Cookie[]cookies=request.getCookies();
        if(cookies!=null){
            Arrays.stream(cookies).forEach(x->{
                System.out.println(x.getName()+":"+x.getValue());
            });
            return  "获取Cookie成功";
        }
        return "cookie为空";

        //for(Cookie c:cookies){
           // System.out.println(c.getName()+":"+c.getValue());
        //}

    }
    @RequestMapping("/getCookie2")
    public  String getCookie2(@CookieValue("bite")String bite){
        return "bite:"+bite;
    }
    @RequestMapping("/setSession")
    public  String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("username","zhangsan");
        return "设置session成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        String username=(String) session.getAttribute("username");
        return "登录用户："+username;
    }

    @RequestMapping("/getSession2")
    public String getSession(HttpSession session){
        String username=(String) session.getAttribute("username");
        return "登录用户："+username;
    }

    @RequestMapping("/getSession3")
    public String getSession(@SessionAttribute(value = "userName",required = false)String userName){

        return "登录用户："+userName;
    }

    @RequestMapping("/getHeader")
    public  String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }
    @RequestMapping("/getHeader2")
    public  String getHeader2(@RequestHeader("User-Agent")String UserAgent){
        return "userAgent:"+userAgent;
    }
}
